/**
 * 
 */
package com.smarthome.api.mobile.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.smarthome.common.mobile.Service;
import com.smarthome.core.area.APIAreaService;
import com.smarthome.core.area.AreaCityService;
import com.smarthome.core.area.AreaDistrictService;
import com.smarthome.core.area.AreaProvinceService;
import com.smarthome.domain.bean.ShAreaCity;
import com.smarthome.domain.bean.ShAreaDistrict;
import com.smarthome.domain.bean.ShAreaProvince;
import com.smarthome.domain.mobile.MobileResponse;
import com.smarthome.domain.mobile.req.MobileAreaRequest;

/**
 * @author dhc
 * @date 2015年10月21日 下午5:32:24
 * @title
 */
@org.springframework.stereotype.Service("mobile.area.service")
public class MobileAreaService implements Service<MobileResponse<?>>{
	
	@Resource(name="apiAreaService")
	private APIAreaService areaService;
	@Resource(name="areaProvinceService")
	private AreaProvinceService provinceService;
	
	@Resource(name="areaCityService")
	private AreaCityService cityService;
	@Resource(name="areaDistrictService")
	private AreaDistrictService districtService;
	
	/***
	 * @title 获取所有的省，市，区（县）
	 * @createTime 2015年10月21日 下午6:13:45
	 * @author dhc
	 */
	public void allProvince(MobileAreaRequest request,MobileResponse<Object> response){
		List<ShAreaProvince> provinces = null;
		if(request.getCascade()){
			provinces = areaService.getAllProvince();
		}  else{
			provinces =  provinceService.queryShAreaProvinces(null);
		}
		response.setData(provinces);
	}

	/**
	 * @title 获取所有的 市、区（县）
	 * @createTime 2015年10月21日 下午6:16:23
	 * @author dhc
	 */
	public void allCity(MobileAreaRequest request,MobileResponse<Object> response){
		List<ShAreaCity> citys =  null;
		if( request.getCascade()){
			citys = 	areaService.getAllCity();
		} else {
			citys = cityService.queryShAreaCitys(null);
		}
		
		response.setData(citys);
	}
	
	/**
	 * @title 所有的区县
	 * @createTime 2015年10月21日 下午6:17:42
	 * @author dhc
	 */
	public void allDistrict(MobileAreaRequest request,MobileResponse<Object> response){
		//response.setData(districtService.queryShAreaDistricts(null));
		response.setData(areaService.getAllDistrict());
	}

	/**
	 * @title 查询，
	 * @createTime 2015年10月21日 下午6:19:00
	 * @author dhc
	 */
	public void province(MobileAreaRequest request,MobileResponse<Object> response){
		List<ShAreaProvince> provinces = new ArrayList<ShAreaProvince>();
		ShAreaProvince province = new ShAreaProvince();
		province.setId(request.getProvinceId());
		province.setProvinceName(request.getProvinceName());
		provinces = provinceService.queryShAreaProvinces(province);
		if(request.getCascade()){
			provinceService.addParam(provinces);
		}
		
		response.setData(provinces);
	}
	
	/**
	 * @title
	 * @createTime 2015年10月21日 下午6:24:10
	 * @author dhc
	 */
	public void city(MobileAreaRequest request,MobileResponse<Object> response){
		List<ShAreaCity > citys = new ArrayList<ShAreaCity>();
		ShAreaCity city = new ShAreaCity();
		city.setId(request.getCityId());
		city.setCityName(request.getCityName());
		city.setProvinceId(request.getProvinceId());
		
		citys = cityService.queryShAreaCitys(city);
		
		if(request.getCascade()){
			cityService.addParam(citys);
		}
		response.setData(citys);
	}

	/**
	 * @title 根据参数，加载 区县数据
	 * @createTime 2015年10月21日 下午6:47:27
	 * @author dhc
	 */
	public void district(MobileAreaRequest request,MobileResponse<Object> response){
		ShAreaDistrict district = new ShAreaDistrict();
		district.setId(request.getDistrictId());
		district.setDistrictName(request.getDistrictName());
		district.setCityId(request.getCityId());
		
		response.setData(districtService.queryShAreaDistricts(district));
	}
	
	
}
